package jju.game;

import javax.swing.*;
import java.awt.*;

public class ScoreThread extends Thread{
    Hero play1, play2;
    Score score;
    ScoreThread(Hero play1, Hero play2, Score score){
        this.play1 = play1;
        this.play2 = play2;
        this.score = score;
    }
    @Override
    public void run() {
        GetDistance getDistance = new GetDistance();
        JFrame frame = new JFrame();
        frame.setBounds(200,200,500,500);
        frame.setTitle("Game Over!");

        JLabel label = new JLabel();
        label.setSize(200,200);
        label.setFont(new Font("Serif", Font.PLAIN, 50));
        frame.add(label);

        while(true){
            try {
                for (int i = 0; i < score.point.size(); ++i){
                    if (getDistance.distance(play1.getX(),play1.getY(),play1.r,score.point.get(i).x,score.point.get(i).y,5) == 1){
                        play1.r++;
                        score.point.remove(i);
                        score.point.add(new Point());
                    } else if (getDistance.distance(play2.getX(),play2.getY(),play2.r,score.point.get(i).x,score.point.get(i).y,5) == 1){
                        play2.r++;
                        score.point.remove(i);
                        score.point.add(new Point());
                    } else if (getDistance.distance(play2.getX(),play2.getY(),play2.r,play1.getX(),play1.getY(),play1.r )== 1){
                        play1.MoveThread.interrupt();
                        play2.r += play1.r;
                        play1.r = 0;
                        label.setText("玩家2胜利!");
                        frame.setVisible(true);
                    } else if (getDistance.distance(play2.getX(),play2.getY(),play2.r,play1.getX(),play1.getY(),play1.r )== 2){
                        play2.MoveThread.interrupt();
                        play1.r += play2.r;
                        play2.r = 0;
                        label.setText("玩家1胜利!");
                        frame.setVisible(true);
                    }
                }
                Thread.sleep(15);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
